var workOrderTable = function() {
	var validator;
	return {
		init : function() {
			var tableId = "workOrder_table";
			util.initCheckBox(tableId);
			var checkBoxDivStr = '<div class="checker" onclick="util.selectCheckBox(\'' + tableId + '\', this)"><span><input type="checkbox" class="checkboxes"></span></div>';

			//查询字典信息列表
			function queryData(sSource, aoData, fnCallback) {
				aoData.push({
					"name" : "customer",
					"value" : $("#id_cusName").val().trim()
				});
				if($("#id_woCategory").val() != ""){
					if(parseInt($("#id_woCategory").val())>5){
						aoData.push({
							"name" : "woType",
							"value" : "2"
						});
						aoData.push({
							"name" : "woCategory",
							"value" : (parseInt($("#id_woCategory").val())-5)+""
						});
					} else {
						aoData.push({
							"name" : "woType",
							"value" : "1"
						});
						aoData.push({
							"name" : "woCategory",
							"value" : $("#id_woCategory").val()
						});
					}
				}
				
				aoData.push({
					"name" : "woStatus",
					"value" : $("#id_woStatus").val()
				});
				$.ajax({
					"type" : "post",
					"url" : sSource,
					"dataType" : "json",
					"data" : {
						"aoData" : JSON.stringify(aoData)
					},
					"success" : function(resp) {
						fnCallback(resp);
					}
				});
			};

			$('#workOrder_table').DataTable({
				"processing" : true,
				"serverSide" : true,
				"sAjaxSource" : "main/wo/getWorkOrder.mvc",
				"fnServerData" : queryData,
				"pageLength" : 10,
				"sDom" : "<'row-fluid'r>t<'row-fluid'<'span6'i><'span6'p>>",
				"oLanguage" : util.getDToLanguage(),
				"columns" : [ {
					"data" : "pkId",
					"render" : function(data, type, full, meta) {
						 return '<div class="checker" onclick="util.selectCheckBox(\'' + tableId + '\', this)"><span><input type="checkbox" class="checkboxes"/></span></div>';
					},
					"bSortable" : false
				},
				{
					"data" : "woCode",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "woStatus",
					"bSortable" : false,
					"render" : function(data, type, full, meta) {
						if("1"==data){
							return "未处理";
						} else if("2"==data){
							return "已确认";
						} else if("3"==data) {
							return "处理中";
						} else if("4"==data) {
							return "已解决";
						} else if("5"==data) {
							return "已关闭";
						} else {
							return null;
						}
					},
					"defaultContent": ""
				},
				{
					"data" : "woCategory",
					"bSortable" : false,
					"render" : function(data, type, full, meta) { 
						if(full.woType == '1'){
							if("1"==data){
								return "报警处理";
							} else if("2"==data){
								return "故障解决";
							} else if("3"==data) {
								return "维保服务";
							} else if("4"==data) {
								return "培训服务";
							} else if("5"==data) {
								return "其他";
							} else {
								return null;
							}
						}else if(full.woType == '2') {
							if("1"==data){
								return "巡检计划";
							} else if("2"==data){
								return "维保计划";
							} else if("3"==data) {
								return "培训计划";
							} else {
								return null;
							}
						}else {
							return null;
						}
						
					},
					"defaultContent": ""
				},
				{
					"data" : "woImportance",
					"bSortable" : false,
					"render" : function(data, type, full, meta) { 
						if("1"==data){
							//return "一般";
							return "<span class=\"label label-info\">一般</span>";
						} else if("2"==data){
							//return "中等";
							return "<span class=\"label label-success\">中等</span>";
						} else if("3"==data) {
							//return "紧急";
							return "<span class=\"label label-important\">紧急</span>";
						} else {
							return null;
						}
					},
					"defaultContent": ""
				},
				{
					"data" : "woSubmitter",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "woCustomer",
					"bSortable" : false,
					"defaultContent": "",
					"sWidth":"170px"
				},
				{
					"data" : "woContent",
					"bSortable" : false,
					"defaultContent": "",
					"sWidth":"200px"
				},
				{
					"data" : "woStartDateStr",
					"bSortable" : false,
					"defaultContent": "",
					"sWidth":"170px"
				},
				{
					"data" : "woOperator",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "updateDateStr",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"bSortable" : false,
					"render" : function(data, type, full, meta) {
						 return '<a class="showDetail" style="cursor:pointer;color:blue;" data-toggle="modal">查看</a>';
					}
				},
				{
					"data" : "refId",
					"bSortable" : false,
					"visible" : false
				}
				],
				"fnDrawCallback" : function(oSetting) {
					util.clearSelected(tableId);
				}
			});

			//打开详情页
			$("#workOrder_table").on("click", "a.showDetail",function(e){
				var d = [];
				d = util.getCheckedData("workOrder_table");
				if (d.length != 1) {
					util.showModal("#_alertModal", "请选择一条记录进行操作！");
					return false;
				} 
				util.showContent('pages/system/workOrder_detail.jsp',{"woId":d[0].pkId,"woType":d[0].woType})

			});

			
			



			

			// 查询
			$("#btn_query").click(function(e) {
				var table = $('#workOrder_table').DataTable();
				table.draw(true);
			});
			// 重置
			$("#btn_reset").click(function() {
				$("#customer_search_form")[0].reset();
				$(".select2-selection__rendered").empty();
				$(".select2-selection__rendered").append("<span class='select2-selection__placeholder'>请选择</span>");
			});
			

			
			


			
			$(function(){
					
				util.initCustomerSelect("id_cusName","220px");			
				$('.search-body input').bind('keyup', function(event) {
					if (event.keyCode == "13") {
						//回车执行查询
						$('.search-button #btn_query').click();
					}
				});
	
			});


			
		}
	};
}();